import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'

import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import users from '../components/user/users.vue'
import personalCenter from '../components/user/personalCenter.vue'
import RoleList from '../components/role/RoleList.vue'
import RoleUpt from '../components/role/upt.vue'
import Model from '../components/model/ModelList.vue'
import logList from '../components/log/logList'
import logDetailAll from '../components/log/logDetail'
import dictListInfo from '../components/dict/dictList'
import andDictInfo from '../components/dict/dictSaveAndEdit'
import SystemParametersInfo from '../components/systemParameters/SystemParametersList'
import employeeAddEdit from '../components/user/employeeAddEdit'
import ApiPurview from '../components/apiPurview/index'
import ApiPurviewUptIndex from '../components/apiPurview/uptIndex'
import generatorFile from '../components/generator/index'

import store from "@/store";


Vue.use(VueRouter)

const logs = {path: '/logList', component: logList}
const personalCenterAll = {path: '/personalCenter', component: personalCenter}
const usersRouter = {path: '/users', component: users}
const rolesRouter = {path: '/roles', component: RoleList}
const resumeRouter = {path: '/resume', component: RoleUpt}
const efaultModue = {path: '/', component: Welcome}
const modelRouter = {path: '/model', component: Model}
const logDetailRouter = {path: '/logDetail', component: logDetailAll}
const dictList = {path: '/dict', component: dictListInfo}
const apipurview = {path: '/apipurview', component: ApiPurview}
const generatorView = {path: '/generator', component: generatorFile}

const systemParametersList = {path: '/systemParameters', component: SystemParametersInfo}


const udgmentAuthority = {
    'users': usersRouter,
    'roles': rolesRouter,
    'resume': resumeRouter,
    'model': modelRouter,
    'logList': logs,
    'personalCenter': personalCenterAll,
    'dict': dictList,
    'systemParameters': systemParametersList,
    'generator': generatorView,
    'apipurview': apipurview,
    '#': efaultModue,
    '/': efaultModue
}


const routes = [
    {path: '/login', component: Login},
    {path: '/', redirect: "/welcome"},
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [
            {
                path: "/welcome",
                component: Welcome
            }, {
                path: "/dictAddAndDict",
                component: andDictInfo
            }, {
                path: "/userEdit",
                component: employeeAddEdit
            }, resumeRouter, {
                path: "/ApiPurviewUptIndex",
                component: ApiPurviewUptIndex
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    // debugger
    if (to.path === '/login') return next();
    const tokenStr = window.sessionStorage.getItem("sign")
    if (!tokenStr) return next('/login')
    next()
})

export function initRouter() {

    const AllRouters = router.options.routes
    const modueListAA = store.state.modueList
    modueListAA.forEach(itme => {
        // console.log(itme)
        itme.children.forEach(two => {
            // console.log(two)
            two.children.forEach(upt => {
                const temp = udgmentAuthority[upt.menuPath]
                // console.log(upt.menuPath)
                if (temp != undefined && temp != null) {
                    AllRouters[2].children.push(temp)
                }

            })
        })
    })
    // //console.log(AllRouters)
    AllRouters[2].children.push(logDetailRouter)
    router.addRoutes(AllRouters)
}


export default router
